1650 // Segments in proc->gdt.
1651 // Also known to bootasm.S and trapasm.S
1652 #define SEG_KCODE 1  // kernel code
1653 #define SEG_KDATA 2  // kernel data+stack
1654 #define SEG_KCPU  3  // kernel per-cpu data
1655 #define SEG_UCODE 4  // user code
1656 #define SEG_UDATA 5  // user data+stack
1657 #define SEG_TSS   6  // this process's task state
1658 #define NSEGS     7
1659 
1660 // Per-CPU state
1661 struct cpu {
1662   uchar id;                    // Local APIC ID; index into cpus[] below
1663   struct context *scheduler;   // Switch here to enter scheduler
1664   struct taskstate ts;         // Used by x86 to find stack for interrupt
1665   struct segdesc gdt[NSEGS];   // x86 global descriptor table
1666   volatile uint booted;        // Has the CPU started?
1667   int ncli;                    // Depth of pushcli nesting.
1668   int intena;                  // Were interrupts enabled before pushcli?
1669 
1670   // Cpu-local storage variables; see below
1671   struct cpu *cpu;
1672   struct proc *proc;
1673   int time;
1674 };
1675 
1676 extern struct cpu cpus[NCPU];
1677 extern int ncpu;
1678 extern int sys_uptime(void);
1679 // Per-CPU variables, holding pointers to the
1680 // current cpu and to the current process.
1681 // The asm suffix tells gcc to use "%gs:0" to refer to cpu
1682 // and "%gs:4" to refer to proc.  ksegment sets up the
1683 // %gs segment register so that %gs refers to the memory
1684 // holding those two variables in the local cpu's struct cpu.
1685 // This is similar to how thread-local variables are implemented
1686 // in thread libraries such as Linux pthreads.
1687 extern struct cpu *cpu asm("%gs:0");       // This cpu.
1688 extern struct proc *proc asm("%gs:4");     // Current proc on this cpu.
1689 
1690 // Saved registers for kernel context switches.
1691 // Don't need to save all the segment registers (%cs, etc),
1692 // because they are constant across kernel contexts.
1693 // Don't need to save %eax, %ecx, %edx, because the
1694 // x86 convention is that the caller has saved them.
1695 // Contexts are stored at the bottom of the stack they
1696 // describe; the stack pointer is the address of the context.
1697 // The layout of the context matches the layout of the stack in swtch.S
1698 // at the "Switch stacks" comment. Switch doesn't save eip explicitly,
1699 // but it is on the stack and allocproc() manipulates it.
1700 struct context {
1701   uint edi;
1702   uint esi;
1703   uint ebx;
1704   uint ebp;
1705   uint eip;
1706 };
1707 
1708 enum procstate { UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE };
1709 
1710 // Per-process state
1711 struct proc {
1712   uint sz;                     // Size of process memory (bytes)
1713   pde_t* pgdir;                // Linear address of proc's pgdir
1714   char *kstack;                // Bottom of kernel stack for this process
1715   enum procstate state;        // Process state
1716   volatile int pid;            // Process ID
1717   struct proc *parent;         // Parent process
1718   struct trapframe *tf;        // Trap frame for current syscall
1719   struct context *context;     // Switch here to run process
1720   void *chan;                  // If non-zero, sleeping on chan
1721   int killed;                  // If non-zero, have been killed
1722   struct file *ofile[NOFILE];  // Open files
1723   struct inode *cwd;           // Current directory
1724   char name[16];               // Process name (debugging)
1725 
1726   int ctime; //creation time
1727   int rtime; //running time
1728   int etime; //end time
1729 
1730   int priority;
1731 };
1732 
1733 // Process memory is laid out contiguously, low addresses first:
1734 //   text
1735 //   original data and bss
1736 //   fixed-size stack
1737 //   expandable heap
1738 
1739 
1740 
1741 
1742 
1743 
1744 
1745 
1746 
1747 
1748 
1749 
